package org.skydiveapp2;

import org.skydiveapp2.ui.InterfaceManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class RigComponentEditActivity extends Activity
{
	// requests
    private static final int REQUEST_NOTES = 0;
    
	// data
	private String notes = "";
	private Boolean isEdit = false;
	
	// listeners
	private OnClickListener notesEditListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{		
			Intent intent = new Intent(RigComponentEditActivity.this, NotesEditActivity.class);
            intent.putExtra(Constants.EXTRA_NOTES, notes);
            startActivityForResult(intent, REQUEST_NOTES);
		}
	};
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
        
		overridePendingTransition(R.anim.fadein,
				R.anim.hold);
		
        // init ui
        initializeUI();
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
            if (requestCode == REQUEST_NOTES && resultCode == RESULT_OK)
            {
                    String notes = intent.getStringExtra(Constants.EXTRA_NOTES);
                    updateNotes(notes);
            }
    }
	
	private void initializeUI()
	{
		// show view
		setContentView(R.layout.rig_component_edit_view);
		
		// set subtitle text
		if (this.findViewById(R.id.tabletLayout) != null)
		{
			InterfaceManager.getInstance().configureTabletUI(this, 
					R.string.rig_component_header);
		}
		else
		{
			InterfaceManager.getInstance().configurePhoneUI(this, 
					R.string.rig_component_header);
		}
		
		// get data
		Bundle extras = getIntent().getExtras();
		isEdit = (extras != null && extras.getBoolean(Constants.EXTRA_RIG_COMPONENT_IS_EDIT, false));
		String name = (extras != null && extras.containsKey(Constants.EXTRA_RIG_COMPONENT_NAME)) ?
		                extras.getString(Constants.EXTRA_RIG_COMPONENT_NAME) : "";
		String serialNumber = (extras != null && extras.containsKey(Constants.EXTRA_RIG_COMPONENT_SERIAL_NUMBER)) ?
		                extras.getString(Constants.EXTRA_RIG_COMPONENT_SERIAL_NUMBER) : "";
		notes = (extras != null && extras.containsKey(Constants.EXTRA_RIG_COMPONENT_NOTES)) ?
		                extras.getString(Constants.EXTRA_RIG_COMPONENT_NOTES) : "";
		
		// set name
		EditText nField = (EditText)findViewById(R.id.RigComponentNameField);
		nField.setText(name);
		
		// set serial number
		EditText snField = (EditText)findViewById(R.id.RigComponentSerialNumberField);
		snField.setText(serialNumber);
		
		// set notes
		TextView notesField = (TextView)findViewById(R.id.RigComponentNotesField);
		notesField.setOnClickListener(notesEditListener);
		notesField.setText(notes);
	}
	
	public void updateNotes(String notes)
	{
		this.notes = notes;
        
        // update UI
        TextView notesField = (TextView)findViewById(R.id.RigComponentNotesField);
        notesField.setText(notes);
	}
	
    private void save()
    {
		// get name from field
		EditText nField = (EditText)findViewById(R.id.RigComponentNameField);
		String name = nField.getText().toString();
		
		// get serial number from field
		EditText snField = (EditText)findViewById(R.id.RigComponentSerialNumberField);
		String serialNumber = snField.getText().toString();
		
		// set result
		Intent result = new Intent();
		result.putExtra(Constants.EXTRA_RIG_COMPONENT_NAME, name);
		result.putExtra(Constants.EXTRA_RIG_COMPONENT_SERIAL_NUMBER, serialNumber);
		result.putExtra(Constants.EXTRA_RIG_COMPONENT_NOTES, notes);
		
		setResult(RESULT_OK, result);
		
		// finish
		close();
    }
	
	private void delete()
	{
        Intent result = new Intent();
        result.putExtra(Constants.EXTRA_RIG_COMPONENT_DELETE, true);
        setResult(RESULT_OK, result);
        
        // finish
        close();
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_save, menu);
	    
		if (isEdit)
			inflater.inflate(R.menu.menu_delete, menu);
		
		inflater.inflate(R.menu.menu_back, menu);
	    return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId())
        {
        case R.id.menu_save:
    		save();
            return true;
            
        case R.id.menu_back:
			close();
            return true;
            
        case R.id.menu_delete:
        	delete();
            return true;
            
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    private void close()
    {
    	finish();
		overridePendingTransition(R.anim.hold,
				R.anim.fadeout);
    }
    
    @Override
    public void onBackPressed()
    {
    	save();
    }
}
